//
//  CLLoginInViewController.m
//  chanlin
//
//  Created by Ryan on 14-11-22.
//  Copyright (c) 2014年 chanlin. All rights reserved.
//

#import "CLLoginInViewController.h"
#import "UIView+Utils.h"
#import "BlockUI.h"
#import "CLLoginModel.h"
#import "CLWebVeiwController.h"
#import "CLPushAnimatedRight.h"
#import "WXApi.h"
#import <TencentOpenAPI/QQApiInterface.h>



@interface CLLoginInViewController ()

@property (nonatomic,strong)UIButton *backButton;

@property (nonatomic,strong)UIButton *weixin;
@property (nonatomic,strong)UIButton *qq;
@property (nonatomic,strong)UIButton *sina;

@property (nonatomic,strong) UITextField *account;
@property (nonatomic,strong) UITextField *password;

@property (nonatomic,strong) UIButton *loginbtn;
@property (nonatomic,strong) UIButton *registbtn;

@end

@implementation CLLoginInViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor=RGB(246, 246, 246);
    
    self.backButton=[UIButton buttonWithType:UIButtonTypeCustom];
    [self.backButton setImage:[UIImage imageNamed:@"tb_back.png"] forState:UIControlStateNormal];
    self.backButton.contentEdgeInsets=UIEdgeInsetsMake(7, 7, 7, 7);
    self.backButton.frame=CGRectMake(15.f, 20.f, 44.f, 44.f);
    __weak typeof(self) me=self;
    [self.backButton handleControlEvent:UIControlEventTouchUpInside withBlock:^(id sender) {
        [me.dispatchObj pop];
    }];
    [self.view addSubview:self.backButton];
    
    UILabel *title=[[UILabel alloc] init];
    title.font=[UIFont systemFontOfSize:20];
    title.frame=CGRectMake((GlobleWidth-50.f)/2.f, 30.f, 50.f, title.font.lineHeight);
    title.textColor=RGB(98, 101, 113);
    title.textAlignment=NSTextAlignmentCenter;
    title.text=@"登录";
   
    [self.view addSubview:title];
    
    CALayer *line=[CALayer layer];
    line.backgroundColor=title.textColor.CGColor;
    line.frame=CGRectMake(0, 64.f, GlobleWidth, 0.5);
    [self.view.layer addSublayer:line];
    
    self.weixin=[UIButton buttonWithType:UIButtonTypeCustom];
    [self.weixin setImage:[UIImage imageNamed:@"tb_weixin.png"] forState:UIControlStateNormal];
    self.weixin.frame=CGRectMake((GlobleWidth-180.f)/4.f, 79.f, 60.f, 60.f);
    [self.view addSubview:self.weixin];
    
    
    [self.weixin handleControlEvent:UIControlEventTouchUpInside withBlock:^(id sender) {
     
        
        [[CLLoginModel sharedInstance] loginWeichat:me
                                            uiBlock:^(BOOL success, id responseObj, NSString *message) {
                                                if(success){
                                                    [me.dispatchObj pop];
                                                }else{
                                                    [[CLGloble sharedInstance] showTieAlertMessage:@"提示" subMessage:@"登陆失败"];
                                                }
        }];
    }];
    
    
   
    
    
    
    self.qq=[UIButton buttonWithType:UIButtonTypeCustom];
    [self.qq setImage:[UIImage imageNamed:@"tb_qq.png"] forState:UIControlStateNormal];
    self.qq.frame=CGRectMake(((GlobleWidth-180.f)/4.f)+self.weixin.right, 79.f, 60.f, 60.f);
    [self.view addSubview:self.qq];
    [self.qq handleControlEvent:UIControlEventTouchUpInside withBlock:^(id sender) {
        [[CLLoginModel sharedInstance] loginQQ:me uiBlock:^(BOOL success, id responseObj, NSString *message) {
            if(success){
                [me.dispatchObj pop];
            }else{
                [[CLGloble sharedInstance] showTieAlertMessage:@"提示" subMessage:@"登陆失败"];
            }
        }];
    }];
    
    self.sina=[UIButton buttonWithType:UIButtonTypeCustom];
    [self.sina setImage:[UIImage imageNamed:@"tb_sina.png"] forState:UIControlStateNormal];
    //self.sina.frame=CGRectMake((GlobleWidth-120.f)/3.f, 79.f, 60.f, 60.f);
      self.sina.frame=CGRectMake(((GlobleWidth-180.f)/4.f)+self.qq.right, 79.f, 60.f, 60.f);
    [self.view addSubview:self.sina];
    
    [self.sina handleControlEvent:UIControlEventTouchUpInside withBlock:^(id sender) {
        [[CLLoginModel sharedInstance] loginSina:me uiBlock:^(BOOL success, id responseObj, NSString *message) {
            if(success){
                [me.dispatchObj pop];
            }else{
                [[CLGloble sharedInstance] showTieAlertMessage:@"提示" subMessage:@"登陆失败"];
            }
        }];
        
    }];

    
    
    self.account=[[UITextField alloc] init];
    self.account.frame=CGRectMake(44, self.sina.bottom+30.f, GlobleWidth-88.f, 40);
   // self.account.backgroundColor=RGB(0xcc, 0xcc, 0xcc);
    self.account.layer.borderColor=RGB(0xcc, 0xcc, 0xcc).CGColor;
    self.account.layer.borderWidth=0.5f;
    [self.view addSubview:self.account];
    
    self.password=[[UITextField alloc] init];
    self.password.secureTextEntry=YES;
   // self.password.backgroundColor=RGB(0xcc, 0xcc, 0xcc);
    self.password.frame=CGRectMake(44, self.account.bottom+15.f, GlobleWidth-88.f, 40);
    self.password.layer.borderColor=RGB(0xcc, 0xcc, 0xcc).CGColor;
    self.password.layer.borderWidth=0.5f;
    [self.view addSubview:self.password];
    
    self.loginbtn=[UIButton buttonWithType:UIButtonTypeCustom];
    self.loginbtn.frame=CGRectMake(self.account.left+40, self.password.bottom+15.f, 50.f, 30);
    self.loginbtn.layer.borderColor=[UIColor grayColor].CGColor;
    [self.loginbtn setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    [self.loginbtn setTitle:@"登录" forState:UIControlStateNormal];
    [self.view addSubview:self.loginbtn];
    
    self.registbtn=[UIButton buttonWithType:UIButtonTypeCustom];
    self.registbtn.frame=CGRectMake(self.account.right-90.f, self.password.bottom+15.f, 50.f, 30);
    self.registbtn.layer.borderColor=[UIColor grayColor].CGColor;
    [self.registbtn setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    [self.registbtn setTitle:@"注册" forState:UIControlStateNormal];
    [self.view addSubview:self.registbtn];
    
    if(![WXApi isWXAppInstalled]){
        self.weixin.hidden=YES;
    }
    
//    if(![QQApiInterface isQQInstalled]){
//        self.qq.hidden=YES;
//    }
    
   // __weak typeof(self) me=self;
    [self.loginbtn handleControlEvent:UIControlEventTouchUpInside withBlock:^(id sender) {
        NSString *a=me.account.text;
        NSString *b=me.password.text;
        if(_isStrNULL(a) || _isStrNULL(b)){
            [[CLGloble sharedInstance] showTieAlertMessage:@"用户名或者密码不能为空" subMessage:nil];
            return ;
        }
        
            [[CLLoginModel sharedInstance] loginIn:a password:b uiBlock:^(BOOL success, id responseObj, NSString *message) {
                if(success){
                    [me.dispatchObj pop];
                }else{
                    [[CLGloble sharedInstance] showTieAlertMessage:@"提示" subMessage:@"登陆失败"];
                }
            }];
        
        
        
        
    }];
    
    [self.registbtn handleControlEvent:UIControlEventTouchUpInside withBlock:^(id sender) {
        CLWebVeiwController *web = [CLWebVeiwController new];
        
        web.itemId=@"http://appv2.chanlin.org/e/opclient/appreg.php";
        [[CLPushAnimatedRight sharedInstance] pushController:web];

    }];
    
}





- (BOOL)showPopAction{
    return YES;
}


@end
